﻿namespace BeatEmUp.World.Drawable
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using BeatEmUp.Menu;
    using BeatEmUp.Characters.Enemies;
    using BeatEmUp.HUD;
    using BeatEmUp.Items.Weapons;
    using BeatEmUp.Enums;
    using Microsoft.Xna.Framework.Content;

    public class AnimootedDrawable : Drawable.IDrawable
    {
        protected string textureName;
        protected Texture2D texture;
        protected Vector2 startingPoint = Vector2.Zero;

        protected float timePerFrame;
        protected float timeSinceLastFrame = 0;
        protected Vector2 frameSize;
        protected int frames;
        protected int currentFrame = 0;

        public AnimootedDrawable(string textureName, int frames, Vector2 frameSize, float timePerFrame = 0.5f)
        {
            this.textureName = textureName;
            this.frames = frames;
            this.frameSize = frameSize;
            this.timePerFrame = timePerFrame;
        }

        public static AnimootedDrawable CreateWithStartingPoint(string textureName, int frames, Vector2 frameSize, Vector2 startingPoint, float timePerFrame = 0.5f)
        {
            AnimootedDrawable drawable = new AnimootedDrawable(textureName, frames, frameSize, timePerFrame);
            drawable.startingPoint = startingPoint;

            return drawable;
        }

        public Drawable.IDrawable Clone()
        {
            return (Drawable.IDrawable)this.MemberwiseClone();
        }

        public void LoadContent(ContentManager contentManager)
        {
            this.texture = contentManager.Load<Texture2D>(this.textureName);
        }

        public void Update(GameTime gameTime)
        {
            if (this.timePerFrame <= (this.timeSinceLastFrame += (float)gameTime.ElapsedGameTime.TotalSeconds))
            {
                this.timeSinceLastFrame -= this.timePerFrame;
                this.currentFrame = ++this.currentFrame % this.frames;
            }
        }

        public void Draw(GameManager gameManager, GameTime gameTime, Vector2 position)
        {
            Vector2 framePosition = new Vector2(this.startingPoint.X + this.frameSize.X * this.currentFrame, this.startingPoint.Y);
            Rectangle source = this.getRect(framePosition, this.frameSize);
            Rectangle target = this.getRect(position, this.frameSize);

            GameManager.customResolution.Draw(this.texture, target, source, Color.White);            
        }

        public Vector2 Size
        {
            get
            {
                return this.frameSize;
            }
        }

        protected Rectangle getRect(Vector2 position, Vector2 dimensions)
        {
            Rectangle target = new Rectangle();

            target.X = (int)position.X;
            target.Y = (int)position.Y;

            target.Width = (int)dimensions.X;
            target.Height = (int)dimensions.Y;

            return target;
        }
    }
}
